package com.zenghm.recyclerview.paging;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.zenghm.recyclerview.decoration.SpaceItemDecoration;
import com.zenghm.recyclerview.paging.databinding.ActivityRecyclerviewBinding;

import java.util.ArrayList;
import java.util.List;

public class DragRecyclerViewActivity extends AppCompatActivity {

    private List<String> mDataSet;
    private DragRecyclerViewAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Context context = this;
        ActivityRecyclerviewBinding binding = ActivityRecyclerviewBinding.inflate(LayoutInflater.from(context));
        setContentView(binding.getRoot());
        final RecyclerView recyclerView = binding.recyclerview;

        mDataSet = new ArrayList<>();
        for (int i = 1; i < 20; i++) {
            mDataSet.add("item" + i);
        }

        mAdapter = new DragRecyclerViewAdapter(this, mDataSet);
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new PagingLayoutManager(RecyclerView.HORIZONTAL, 4, 2));
        recyclerView.addItemDecoration(new SpaceItemDecoration(8));

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {

            @Override
            public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                int dragFlag = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT | ItemTouchHelper.UP | ItemTouchHelper.DOWN;
                int swipeFlag = 0;
                return makeMovementFlags(dragFlag, swipeFlag);
            }

            @Override
            public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
                if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
                    viewHolder.itemView.setAlpha(0.6f);
                }
                super.onSelectedChanged(viewHolder, actionState);
            }

            @Override
            public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                viewHolder.itemView.setAlpha(1.0f);
                recyclerView.scrollToPosition(viewHolder.getAdapterPosition());
                super.clearView(recyclerView, viewHolder);
            }

            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                int fromPos = viewHolder.getAdapterPosition();
                int toPos = target.getAdapterPosition();
                String item = mDataSet.remove(fromPos);
                mDataSet.add(toPos, item);
                mAdapter.notifyItemMoved(fromPos, toPos);
                return true;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {

            }
        });
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }
}
